//
//  OPNavigationController.m
//  OnePerson
//
//  Created by BaoWanPei on 16/6/11.
//  Copyright © 2016年 BWP. All rights reserved.
//

#import "OPNavigationController.h"

@interface OPNavigationController ()

@end

@implementation OPNavigationController

#pragma mark - 设置NavigationBar
+ (void)load{
    
    // 获取当前类的NavigationBar
    UINavigationBar *navBar = [UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[self]];
    // 中间标题
    NSMutableDictionary *titleAttributesdict = [NSMutableDictionary dictionary];
    titleAttributesdict[NSFontAttributeName] = [UIFont systemFontOfSize:19];
//    titleAttributesdict[NSForegroundColorAttributeName] = RGB_alphaColor(0, 0, 0, .8);
    [navBar setTitleTextAttributes:titleAttributesdict];
    // 设置背景image
    [navBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
    
    /** 设置UIBarButtonItem */
    UIBarButtonItem *item = [UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[self]];
    // UIControlStateNormal
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSForegroundColorAttributeName] = [UIColor blackColor];
    normalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:17];
    [item setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    // UIControlStateDisabled
    NSMutableDictionary *disabledAttrs = [NSMutableDictionary dictionary];
    disabledAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
    [item setTitleTextAttributes:disabledAttrs forState:UIControlStateDisabled];
}

#pragma mark - 修改返回按钮
// 拦截push过去的控制器 修改其控制器 左上角返回按钮的样式
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    
    if (self.childViewControllers.count > 0) {
        // 隐藏TabBar
        viewController.hidesBottomBarWhenPushed = YES;
        
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        // nromal
        [backBtn setTitle:@"返回" forState:UIControlStateNormal];
        [backBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [backBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
        
        [backBtn setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal];
        [backBtn setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted];
        
        [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        
        [backBtn sizeToFit];
        // 设置按钮内边距
        backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
        
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    }
    
    [super pushViewController:viewController animated:animated];
    //    MyLog(@"%@",self.interactivePopGestureRecognizer);
}

// pop
- (void)back{
    
    [self popViewControllerAnimated:YES];
}

#pragma mark - 设置全屏又滑返回
- (void)viewDidLoad{
    [super viewDidLoad];
    /*
     <UIScreenEdgePanGestureRecognizer: 0x7fb9d0c61d90; state = Possible; delaysTouchesBegan = YES; view = <UILayoutContainerView 0x7fb9d0c5fbd0>; target= <(action=handleNavigationTransition:, target=<_UINavigationInteractiveTransition 0x7fb9d0c614b0>)>>
     */
    // 禁掉父类的手势
    self.interactivePopGestureRecognizer.enabled = NO;
    // 给当前自定义Nav.view添加拖拽手势，实现可全屏滑动
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self.interactivePopGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];
    [self.view addGestureRecognizer:pan];
    
    pan.delegate = self;
    
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    
    return self.childViewControllers.count > 1;
}


@end
